package org.wmq.View;

import org.wmq.Listener.MyKeyListener;
import org.wmq.Mapper.impl.MySimpleMapMapperImpl;
import org.wmq.model.Entity.MySimpleMap;
import org.wmq.model.Entity.MySimpleMapComment;
import org.wmq.service.CommentService;
import org.wmq.service.MySimpleMapService;
import org.wmq.service.impl.CommentServiceImpl;
import org.wmq.service.impl.MySimpleMapServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

public class MyMainFrame {
    static String[] files = new File("D:\\map_collection_data\\map_data").list();
    static int index = 0;
    static private MySimpleMapService mySimpleMapService = new MySimpleMapServiceImpl();
    static private CommentService commentService = new CommentServiceImpl();
    public static void defaultCase() throws IOException {

        Frame frame = new Frame();
        frame.setLocation(new Point(100, 100));
        frame.setSize(300, 300);
        frame.setLayout(new BorderLayout()); // 设置布局管理器

        loadMapAndComment(frame);

        frame.addKeyListener(new MyKeyListener());
        frame.setVisible(true); // 使用setVisible(true)代替show()
    }
    public static void loadMapAndComment(Frame frame) throws IOException {
        // 创建一个Panel来放置图片和文本字段
        Panel panel = new Panel();
        panel.setLayout(new BorderLayout()); // 设置布局管理器
        MySimpleMap mySimpleMap = mySimpleMapService.selectById(Long.parseLong(files[index].split("\\.")[0]));

        // 加载图片
        Image image = Toolkit.getDefaultToolkit().getImage(mySimpleMap.getUrl());
        ImageIcon imageIcon = new ImageIcon(image);
        JLabel imageLabel = new JLabel(imageIcon);
        panel.add(imageLabel);

        // 添加文本字段
        MySimpleMapComment mySimpleMapComment = commentService.selectByMySimpleMapId(mySimpleMap.getId());
        TextField textField = new TextField(mySimpleMapComment.getComment());
        panel.add(textField, BorderLayout.SOUTH); // 将文本字段添加到Panel的底部

        frame.add(panel, BorderLayout.CENTER); // 将Panel添加到Frame的中心

    }
}
